<?php
namespace Budget\BudgetBundle\Services;

use Budget\BudgetBundle\Entity\Family;
use Doctrine\ORM\EntityManager;
use Budget\BudgetBundle\Helper\DateHelper;
use Budget\BudgetBundle\Model\Charts\ChartExpenses;
use Budget\BudgetBundle\Model\Charts\ChartIncomes;
use Budget\BudgetBundle\Entity\User;

class ChartService
{
    protected $entityManager;
    
    public function __construct(EntityManager $entityManager) 
    {
        $this->entityManager = $entityManager;
    }
    
    public function getExpensesToJson($user, $dateToDB, $dateFromDB, $where, $whereParameters)
    {
        $chart        = new ChartExpenses($this->entityManager);
        $expenses     = $chart->getDataForFamilyToArray($user->getFamily(), null, null, 'e.date_created', null, array('where' => $where, 'parameters' => $whereParameters));
        $expensesJson = $chart->getToDataJson($expenses, $dateToDB, $dateFromDB);
        
        return $expensesJson;
    }
    
    public function getIncomesToJson($user, $dateTo, $dateFrom)
    {
        $chart        = new ChartIncomes($this->entityManager, $user->getFamily());
        $dateToDB     = DateHelper::convertToYearMonth($dateTo);
        $dateFromDB   = DateHelper::convertToYearMonth($dateFrom);
        
        $incomes      = $chart->getDataForFamilyToArray($user->getFamily(), null, null, 'i.date_created', null, $dateTo, $dateFrom);
        $incomesJson  = $chart->getToDataJson($incomes, $dateToDB, $dateFromDB);
        
        return $incomesJson;
    }
}

